package com.wt.admin.service.ai.impl.agents;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.wt.admin.domain.dto.ai.AgentsInfoDTO;
import com.wt.admin.domain.entity.ai.MCPEntity;
import com.wt.admin.domain.entity.ai.ModelConfigEntity;
import com.wt.admin.service.ai.impl.mcp.MCPStart;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.tool.ToolCallbackProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface AgentsBuilderService {


    AgentsManager.ChatClientMapper builder(AgentsInfoDTO agents, ModelConfigEntity model, List<MCPEntity> mcpEntities);


    default ToolCallbackProvider[] getToolCallbackProvider(MCPStart mcpStart, MCPEntity mcpEntities) {
        return switch (mcpEntities.getTag()) {
            case 0 -> new ToolCallbackProvider[]{
                    mcpStart.builderMcpClientSTDIO(mcpEntities.getContent(), mcpEntities.isSync())
            };
            case 1 -> {
                List<Map> configList = JSONUtil.toList(mcpEntities.getContent(), Map.class);
                yield configList.stream()
                        .map(k -> mcpStart.builderMcpClientSSE(
                                k.get("server").toString(),
                                k.get("url").toString(),
                                mcpEntities.isSync()
                        ))
                        .toArray(ToolCallbackProvider[]::new);
            }
            case 2 -> {
                List<Map> configList = JSONUtil.toList(mcpEntities.getContent(), Map.class);
                yield configList.stream()
                        .map(k -> mcpStart.builderMcpClientFlux(
                                k.get("server").toString(),
                                k.get("url").toString(),
                                mcpEntities.isSync()
                        ))
                        .toArray(ToolCallbackProvider[]::new);
            }
            default -> new ToolCallbackProvider[0];
        };
    }

    default AgentsManager.ChatClientMapper build(ChatModel chatModel, AgentsInfoDTO agents, List<MCPEntity> mcpEntities, MCPStart mcpStart, ChatOptions options){
        ChatClient.Builder builder = ChatClient.builder(chatModel)
                .defaultSystem("""
                        你是%s,你的名字是%s,%s
                        """.formatted(
                        Optional.ofNullable(agents.getName()).orElse(""),
                        Optional.ofNullable(agents.getName()).orElse(""),
                        Optional.ofNullable(agents.getBackground()).orElse(""))
                );
        List<ToolCallbackProvider> allTools = new ArrayList<>();
        mcpEntities.forEach(mcpEntity -> {
            ToolCallbackProvider[] toolCallbackProvider = getToolCallbackProvider(mcpStart, mcpEntity);
            if (!ObjectUtil.isEmpty(toolCallbackProvider)) {
                allTools.addAll(List.of(toolCallbackProvider));
            }
        });
        if (!allTools.isEmpty()) {
            builder.defaultTools(allTools.toArray(new ToolCallbackProvider[0]));
        }
        // 不建议使用工具，1. 很多大模型不支持 2. 不方便与外部建立连接
//        ToolCallback[] dateTimeTools = ToolCallbacks.from(new DateTimeTools(),new WeatherTools());
        return new AgentsManager.ChatClientMapper(builder.build(),options);
    }


}
